//
//  AddViewController.m
//  one class
//
//  Created by M on 17/9/22.
//  Copyright © 2017年 1512. All rights reserved.
//

#import "AddViewController.h"
#import "ViewController.h"
@interface AddViewController ()
{
    NSUserDefaults *userDefatluts ;
}
@property (strong, nonatomic) UITextField *text ;
@property (strong, nonatomic) UITextField *text2 ;

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    userDefatluts = [NSUserDefaults standardUserDefaults];
    self.title = @"添加用户";
    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(save)];
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor whiteColor];
    _text = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, 320, 35)];
    _text2 = [[UITextField alloc]initWithFrame:CGRectMake(0, 140, 320, 35)];
  
    
    //设置边框样式，只有设置了才会显示边框样式
    _text.borderStyle = UITextBorderStyleNone;
    _text2.borderStyle = UITextBorderStyleNone;
    
    //当输入框没有内容时，水印提示 提示内容为password
    _text.placeholder = @"姓名";
    _text2.placeholder = @"号码";
    
    //设置输入框内容的字体样式和大小
    _text.font = [UIFont fontWithName:@"Arial" size:18.0f];
    _text2.font = [UIFont fontWithName:@ "Arial" size:18.0f];
    
    //输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
    _text.clearButtonMode = UITextFieldViewModeAlways;
    _text2.clearButtonMode = UITextFieldViewModeAlways;

    [self.view addSubview:_text];
    [self.view addSubview:_text2];

}

-(void)save
{
    
    //[NSUserDefaults setObject:"张三"　forKey:@"name"];
    //[userDefaults synchronize];
    BOOL hasSave = YES;
    NSDictionary *dict = @{
        @"name":_text.text,
        @"tel":_text2.text
    };
    
    NSString *message;
    userDefatluts = [NSUserDefaults standardUserDefaults];
    NSMutableArray *arr = [NSMutableArray arrayWithArray:[userDefatluts objectForKey:@"phoneMans"]];
    for(NSDictionary *dit in arr){
        NSString * name = [dit objectForKey:@"name"];
        if(name == [dict objectForKey:@"name"]){
            hasSave = NO;
            break ;
           
        }
    }
    
    if(hasSave){
        [arr addObject:dict];
        [userDefatluts setObject:arr forKey:@"phoneMans"];
        message = @"添加成功";
    }else{
         message = @"联系人已存在";
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
   
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive  handler:^(UIAlertAction * _Nonnull action) {
        if([message  isEqual: @"添加成功"]){
            [self.navigationController popViewControllerAnimated:YES];
        }
        NSLog(@"联系人已存在");
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
